package com.yiyi.credit.manage.sso.service.inner;


import com.yiyi.credit.manage.sso.cache.RedisManager;
import com.yiyi.credit.manage.sso.common.Constant;
import com.yiyi.credit.manage.sso.common.exception.ResponseCode;
import com.yiyi.credit.manage.sso.common.exception.ServiceException;
import com.yiyi.credit.manage.sso.common.page.PageUtils;
import com.yiyi.credit.manage.sso.common.page.Pagination;
import com.yiyi.credit.manage.sso.mapper.SysRoleMapper;
import com.yiyi.credit.manage.sso.mapper.SysUserMapper;
import com.yiyi.credit.manage.sso.model.entity.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;


@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    SysRoleMapper sysRoleMapper;

    @Autowired
    RedisManager redisManager;


    @Override
    public int addSysRole(SysRole sysRole) throws ServiceException {

        //判断当前系统的角色名称是否已经存在
        List<SysRole> list = findSysRoleByCondition(sysRole.getSysId(), sysRole.getRoleName());
        if (null != list && list.size() > Constant.INT_ZERO) {
            throw new ServiceException(ResponseCode.ROLE_EXIST);
        }
        sysRole.setCreateTime(new Date().getTime());
        sysRole.setUpdateTime(new Date().getTime());
        return sysRoleMapper.insertSelective(sysRole);
    }


    @Override
    public int updateSysRole(SysRole sysRole) throws ServiceException {

        //判断当前系统的角色名称是否已经存在
        List<SysRole> list = findSysRoleByCondition(sysRole.getSysId(), sysRole.getRoleName());
        if (null == list && list.size() == Constant.INT_ZERO) {
            throw new ServiceException(ResponseCode.NO_SYS_ROLE);
        }
        sysRole.setUpdateTime(new Date().getTime());
        return sysRoleMapper.  updateByPrimaryKeySelective(sysRole);
    }


    @Override
    public int deleteSysRoleById(SysRole sysRole) throws ServiceException {

        return sysRoleMapper.delete(sysRole);
    }


    @Override
    public SysRole getSysRoleById(Long id) throws ServiceException {
        return sysRoleMapper.selectByPrimaryKey(id);
    }


    @Override
    public List<SysRole> getSysRoleBySysId(Long sysInfoId) throws ServiceException {
        Example example = new Example(SysRole.class);
        example.createCriteria().andEqualTo("sysId", sysInfoId);
        return sysRoleMapper.selectByExample(example);
    }


    @Override
    public Pagination getSysRolePageBySysId(Long sysInfoId, Pagination pagination) throws ServiceException {
        Long count = sysRoleMapper.getCount(sysInfoId);
        pagination.setRecordsTotal(count);
        pagination.setPage(PageUtils.getPage(count, pagination.getLength()));
        if (count > Constant.LONG_ZERO) {
            pagination.setData(sysRoleMapper.getPageData(sysInfoId, pagination));
        }
        return pagination;
    }


    private List<SysRole> findSysRoleByCondition(Long sysInfoId, String roleName) {
        Example example = new Example(SysRole.class);
        example.createCriteria().andEqualTo("sysId", sysInfoId).andEqualTo("roleName", roleName);
        return sysRoleMapper.selectByExample(example);
    }


    public List<SysRole> getAllSysRole() throws ServiceException {
        return sysRoleMapper.selectAll();
    }



    public List<SysRole> getSysRoleList(Long sysInfoId) throws ServiceException {

        Example example = new Example(SysRole.class);
        example.createCriteria().andEqualTo("sysId", sysInfoId);
        List<SysRole> roleList= sysRoleMapper.selectByExample(example);
        return roleList;
    }




    @Override
    public List<SysRole> getRoleId(Long sysInfoId, String roleName) throws ServiceException {
        Example example = new Example(SysRole.class);
        example.createCriteria().andEqualTo("roleName", roleName).andEqualTo("sysId", sysInfoId);
        return sysRoleMapper.selectByExample(example);
    }

}
